package a66weding.com.jiehuntong.common.utils.dataselect;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.Calendar;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.inner.DataCallBack;
import a66weding.com.jiehuntong.widgets.wheelview.OnWheelChangedListener;
import a66weding.com.jiehuntong.widgets.wheelview.WheelView;
import a66weding.com.jiehuntong.widgets.wheelview.adapters.ArrayWheelAdapter;
import a66weding.com.jiehuntong.widgets.wheelview.adapters.NumericWheelAdapter;

/**
 * Created by admin on 2016/8/12.
 * 时间选择器 ;
 */
public class DataSelectUtil  implements OnWheelChangedListener {
    private WheelView mViewYear;
    private WheelView mViewMonth;
    private WheelView mViewDistrict;
    public  static Context context;
    private PopupWindow mDatePopupWindow;
    private static DataSelectUtil dataSelectUtil;
    private int mCurrentYear;
    private int year;
    private int month;
    private int day;
    private int mCurrentMonth;
    private int mCurrentDay;
    private int mStartYear = 2016;
    private int mCurrMonth;
    private int mSelectDay;
    private int flag;
    //结果
    private String weddingDate;

    private DataCallBack callBack;
    public static DataSelectUtil getInstance( ){

        if (dataSelectUtil ==null){
            dataSelectUtil = new DataSelectUtil();
        }
        return  dataSelectUtil;
    }
    /**
     * 设置婚期的popupWindow
     * @param view
     */
   public  void showdatePopupWindow(View view, Context context, final DataCallBack callBack, final int flag) {
        // 一个自定义的布局，作为显示的内容
       this.context = context;
       this.callBack = callBack;
       this.flag = flag;
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.pop_tools_holiday_day_date, null);
        TextView mCancelTv = (TextView) contentView.findViewById(R.id.tv_cancel);
        TextView mConfimTv = (TextView) contentView.findViewById(R.id.tv_confim);
        mViewYear = (WheelView) contentView.findViewById(R.id.id_hour);
        mViewMonth = (WheelView) contentView.findViewById(R.id.id_minute);
        mViewDistrict = (WheelView) contentView.findViewById(R.id.id_day);

        mCancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDatePopupWindow != null) {
                    mDatePopupWindow.dismiss();
                    mDatePopupWindow = null;
                }
            }
        });
        mConfimTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                weddingDate = year + "-" +  month + "-" + day;
                callBack.sendData(weddingDate,flag);
                if (mDatePopupWindow != null) {
                    mDatePopupWindow.dismiss();
                    mDatePopupWindow = null;
                }
            }
        });

        // 添加change事件
        mViewDistrict.addChangingListener(this);
        // 添加change事件
        mViewYear.addChangingListener(this);
        // 添加change事件
        mViewMonth.addChangingListener(this);

        setUpData();
        mDatePopupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

        mDatePopupWindow.setBackgroundDrawable(new ColorDrawable(
                Color.TRANSPARENT));

        //点击空白处时，隐藏掉pop窗口
        mDatePopupWindow.setTouchable(true);

        mDatePopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        //在PopupWindow里面就加上下面代码，让键盘弹出时，不会挡住pop窗口。
        mDatePopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    }
    private int mSelectYear;
    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewYear) {
            mSelectYear = newValue;

            if ((mStartYear + mSelectYear) < mCurrentYear) {
                mViewYear.setCurrentItem(mCurrentYear - mStartYear);
            }
            updateDays();

        } else if (wheel == mViewMonth) {

            updateDays();
        } else if (wheel == mViewDistrict) {
            mSelectDay = newValue;
            updateDays();
        }
    }

    /**
     * 设置婚期的日期
     */
    private void setUpData() {
        getTime();
        String[] years = new String[11];
        for (int i = 0; i < 11; i++) {
            years[i] = 2016 + i + "年";
        }
        ArrayWheelAdapter yearWheelAdapter = new ArrayWheelAdapter(context, years);

        String[] months = new String[12];
        for (int i = 0; i < 12; i++) {
            months[i] = 1 + i + "月";
        }
        ArrayWheelAdapter montnWheelAdapter = new ArrayWheelAdapter<String>(context, months);

        mViewYear.setViewAdapter(yearWheelAdapter);
        mViewMonth.setViewAdapter(montnWheelAdapter);

        // 设置可见条目数量
        mViewDistrict.setVisibleItems(2);
        mViewYear.setVisibleItems(7);
        mViewMonth.setVisibleItems(7);

        int index = mCurrentYear - mStartYear;
        mViewYear.setCurrentItem(index);

        mViewDistrict.setCurrentItem(0);
        mViewYear.setCurrentItem(0);
        mViewMonth.setCurrentItem(mCurrentMonth);

        mViewYear.setCyclic(true);
        mViewMonth.setCyclic(true);

        updateDays();

    }
    /**
     * 设置每天的方法
     */
    private void updateDays() {
        year = mViewYear.getCurrentItem() + mStartYear;
        month = mViewMonth.getCurrentItem() + 1;

        String[] mDays = null;
        int lastDay = 30;

        if (month == 2) {
            if (isLeapYear(year)) {
                mDays = new String[29];
                for (int i = 1; i < 29; i++) {
                    mDays[i] = i + "日";
                }
                lastDay = 29;
            } else {
                mDays = new String[28];
                for (int i = 1; i < 28; i++) {
                    mDays[i] = i + "日";
                }
                lastDay = 28;
            }
        } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            mDays = new String[31];
            for (int i = 1; i < 31; i++) {
                mDays[i] = i + "日";
            }
            lastDay = 31;

        } else {
            mDays = new String[30];
            for (int i = 1; i < 30; i++) {
                mDays[i] = i + "日";
            }
            lastDay = 30;
        }
        if (mDays == null) {
            mDays = new String[]{""};
        }
        day = mViewDistrict.getCurrentItem() + 1;

        NumericWheelAdapter dayWheelAdapter = new NumericWheelAdapter(context, 1, lastDay);

        mViewDistrict.setViewAdapter(dayWheelAdapter);

        mViewDistrict.setVisibleItems(7);

        if (mSelectDay > mDays.length - 1) {
            mSelectDay = mDays.length - 1;
        }

        mViewDistrict.setCurrentItem(mSelectDay);

        mViewDistrict.setCyclic(true);

    }
    /**
     * 获取系统时间
     */
    public void getTime(){
        Calendar c = Calendar.getInstance();
//        取得系统日期:
        mCurrentYear = c.get(Calendar.YEAR);
        mCurrentMonth = c.get(Calendar.MONTH);
        mCurrentDay = c.get(Calendar.DAY_OF_MONTH);
        mSelectDay=mCurrentDay-1;

        //取得系统时间：
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int  minute = c.get(Calendar.MINUTE);

    }
    /**
     * 判断是否是闰年
     * @param year
     * @return
     */
    public static boolean isLeapYear(int year) {
        boolean flag = false;
        if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
            flag = true;
        }
        return flag;
    }
}
